import request from "@/utils/request";

const CUSTOMER_BASE_URL = "/business/customer";

const CustomerAPI = {
  /** 获取客户分页数据 */
  getPage(queryParams?: CustomerPageQuery) {
    return request<any, PageResult<CustomerPageVO[]>>({
      url: `${CUSTOMER_BASE_URL}/loadPage`,
      method: "get",
      params: queryParams,
    });
  },

  /** 获取客户下拉数据源 */
  getOptions() {
    return request<any, CustomerPageVO[]>({
      url: `${CUSTOMER_BASE_URL}/loadAll`,
      method: "get",
    });
  },
  /**
   * 更新客户的标签
   *
   * @param customerId 客户ID
   * @returns 客户的标签集合
   */
  updateTag(customerId: string, tags: Array<string>) {
    let data = {
      customerId: customerId,
      tags: tags,
    };
    return request<any, string[]>({
      url: `${CUSTOMER_BASE_URL}/updateTags`,
      method: "post",
      data,
    });
  },

  /**
   * 分配菜单权限
   *
   * @param roleId 客户ID
   * @param data 菜单ID集合
   */
  updateRoleMenus(roleId: string, data: number[]) {
    let params = { roleId: roleId, menuIds: data };
    return request({
      url: `${CUSTOMER_BASE_URL}/roleMenu`,
      method: "post",
      data: params,
    });
  },

  /**
   * 获取客户表单数据
   *
   * @param id 客户ID
   * @returns 客户表单数据
   */
  getFormData(id: string) {
    return request<any, CustomerForm>({
      url: `${CUSTOMER_BASE_URL}/load/${id}`,
      method: "get",
    });
  },

  /** 添加客户 */
  create(data: CustomerForm) {
    return request({
      url: `${CUSTOMER_BASE_URL}/add`,
      method: "post",
      data,
    });
  },

  /**
   * 更新客户
   *
   * @param id 客户ID
   * @param data 客户表单数据
   */
  update(id: string, data: CustomerForm) {
    return request({
      url: `${CUSTOMER_BASE_URL}/update`,
      method: "post",
      data,
    });
  },

  /**
   * 批量删除客户，多个以英文逗号(,)分割
   *
   * @param ids 客户ID字符串，多个以英文逗号(,)分割
   */
  deleteByIds(ids: string) {
    return request({
      url: `${CUSTOMER_BASE_URL}/batchDelete?ids=${ids}`,
      method: "get",
    });
  },
};

export default CustomerAPI;

/** 客户分页查询参数 */
export interface CustomerPageQuery extends PageQuery {
  /** 搜索关键字 */
  keywords?: string;
}

/** 客户分页对象 */
export interface CustomerPageVO {
  /** 客户ID */
  id: string;
  /** 客户编码 */
  name?: string;
  /** 电话 */
  phone?: string;
  /** 邮箱 */
  email?: string;
  /** 地址 */
  address?: string;
  /** 备注*/
  remark?: string;
  /**生日 */
  birthday?: string;
  /**性别 */
  sex?: string;
  /**tags */
  tags: string;
}

/** 客户表单对象 */
export interface CustomerForm {
  /** 客户ID */
  id?: string;
  /** 客户编码 */
  name?: string;
  /** 电话 */
  phone?: string;
  /** 邮箱 */
  email?: string;
  /** 地址 */
  address?: string;
  /** 备注*/
  remark?: string;
  /**生日 */
  birthday?: string;
  /**性别 */
  sex?: string;
  /**
   * 标签
   */
  tags?: string;
}
